home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Sources / UAssociation.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  11.7 KB  |  424 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UAssociation.cp
  3. // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UASSOCIATION__
  7. #include "UAssociation.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __UFAILURE__
  13. #include "UFailure.h"
  14. #endif
  15.  
  16. #ifndef __UITERATOR__
  17. #include "UIterator.h"
  18. #endif
  19.  
  20. #ifndef __ULISTITERATOR__
  21. #include "UListIterator.h"
  22. #endif
  23.  
  24. #ifndef __UCOREUTILITIES__
  25. #include "UCoreUtilities.h"
  26. #endif
  27.  
  28. // Toolbox
  29.  
  30. #ifndef __ERRORS__
  31. #include <Errors.h>
  32. #endif
  33.  
  34. #ifndef __TOOLUTILS__
  35. #include <ToolUtils.h>
  36. #endif
  37.  
  38.  
  39. //========================================================================================
  40. // GLOBAL Procedures
  41. //========================================================================================
  42. static CompareResult CompareEntryKeys(TObject* anItem, void* yourDataPtr);
  43.  
  44. //========================================================================================
  45. // struct CEntryWithKey
  46. //========================================================================================
  47.  
  48. struct CEntryWithKey
  49. {
  50. public:
  51.     // Fields
  52.     const CStr255& fKeyStr;
  53.  
  54.     // Constructor
  55.     CEntryWithKey(const CStr255& thekeyStr) :
  56.         fKeyStr(thekeyStr)
  57.     {
  58.     }
  59. };
  60.  
  61.  
  62. //----------------------------------------------------------------------------------------
  63. // CompareEntryKeys:
  64. //----------------------------------------------------------------------------------------
  65. #pragma segment MAAssociationRes
  66.  
  67. CompareResult CompareEntryKeys(TObject* anItem, void* yourDataPtr)
  68. {
  69.     CEntryWithKey* comparisonInfo = (CEntryWithKey*)yourDataPtr;
  70.     
  71.     if (comparisonInfo->fKeyStr < **(((TEntry *)anItem)->fKey))
  72.         return kItemGreaterThanCriteria;
  73.     else if (comparisonInfo->fKeyStr > **(((TEntry *)anItem)->fKey))
  74.         return kItemLessThanCriteria;
  75.     else
  76.         return kItemEqualCriteria;
  77. } // CompareEntryKeys
  78.  
  79.  
  80. //========================================================================================
  81. // CLASS TEntry
  82. //========================================================================================
  83. #undef Inherited
  84. #define Inherited TObject
  85.  
  86. #pragma segment MAAssociationNonRes
  87. MA_DEFINE_CLASS_M1(TEntry, Inherited);
  88.  
  89. //----------------------------------------------------------------------------------------
  90. // TEntry constructor
  91. //----------------------------------------------------------------------------------------
  92. #pragma segment MAAssociationRes
  93.  
  94. TEntry::TEntry()
  95. {
  96.     fKey = NULL;
  97.     fValue = NULL;
  98. } // TEntry::TEntry
  99.  
  100. //----------------------------------------------------------------------------------------
  101. // TEntry::IEntry:
  102. //----------------------------------------------------------------------------------------
  103. #pragma segment MAAssociationRes
  104.  
  105. void TEntry::IEntry(const CStr255& itsKey,
  106.                            const CStr255& itsValue)
  107. {
  108.     this->IObject();
  109.  
  110.     FailInfo fi;
  111.     Try(fi)
  112.     {
  113.         fKey = (CStringHandle)NewString(itsKey);    // need NewPermString
  114.         FailNIL(fKey);
  115.  
  116.         fValue = (CStringHandle)NewString(itsValue);
  117.         FailNIL(fValue);
  118.         fi.Success();
  119.     }
  120.     else
  121.     {
  122.         this->Free();
  123.         fi.ReSignal();
  124.     }
  125. } // TEntry::IEntry
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // TEntry::Free:
  129. //----------------------------------------------------------------------------------------
  130. #pragma segment MAAssociationRes
  131.  
  132. TEntry::~TEntry()
  133. {
  134.     fKey = (CStringHandle)DisposeIfHandle((Handle)fKey);
  135.     fValue = (CStringHandle)DisposeIfHandle((Handle)fValue);
  136. } // TEntry::Free
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // TEntry::SetValue:
  140. //----------------------------------------------------------------------------------------
  141. #pragma segment MAAssociationRes
  142.  
  143. void TEntry::SetValue(const CStr255& value)
  144. {
  145.     SetString((StringHandle)fValue, (ConstStr255Param)&value);
  146.     if ((**fValue) != value)
  147.         FailOSErr(memFullErr);                    // SetString may attempt to increase the
  148.                                                 //  handle size, yet it does not return
  149.                                                 //  errors!
  150. } // TEntry::SetValue
  151.  
  152.  
  153. //========================================================================================
  154. // CLASS TEntriesList
  155. //========================================================================================
  156. #undef Inherited
  157. #define Inherited TSortedList
  158.  
  159. #pragma segment MAAssociationNonRes
  160. MA_DEFINE_CLASS_M1(TEntriesList, Inherited);
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // TEntriesList: Empty constructor to satisfy the compiler.
  164. //----------------------------------------------------------------------------------------
  165. #pragma segment ConstructorRes
  166.  
  167. TEntriesList::TEntriesList()
  168. {
  169. } // TEntriesList::TEntriesList
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // TEntriesList destructor
  173. //----------------------------------------------------------------------------------------
  174. #pragma segment MADestructorRes
  175.  
  176. TEntriesList::~TEntriesList()
  177. {
  178. }
  179.  
  180. //----------------------------------------------------------------------------------------
  181. // TEntriesList::Compare:
  182. //----------------------------------------------------------------------------------------
  183. #pragma segment MAAssociationRes
  184.  
  185. CompareResult TEntriesList::Compare(TObject* item1,
  186.                                    TObject* item2)
  187. {
  188.     if ((**((TEntry *)item1)->fKey) < (**((TEntry *)item2)->fKey))
  189.         return kItem1LessThanItem2;
  190.     else if ((**((TEntry *)item1)->fKey) > (**((TEntry *)item2)->fKey))
  191.         return kItem1GreaterThanItem2;
  192.     else
  193.         return kItem1EqualItem2;
  194. } // TEntriesList::Compare
  195.  
  196. //----------------------------------------------------------------------------------------
  197. // TEntriesList::IEntriesList:
  198. //----------------------------------------------------------------------------------------
  199. #pragma segment MAAssociationRes
  200.  
  201. void TEntriesList::IEntriesList()
  202. {
  203.     this->ISortedList();
  204. } // TEntriesList::IEntriesList
  205.  
  206.  
  207.  
  208. //========================================================================================
  209. // CLASS TAssociation
  210. //========================================================================================
  211. #undef Inherited
  212. #define Inherited TObject
  213.  
  214. #pragma segment MAAssociationNonRes
  215. MA_DEFINE_CLASS_M1(TAssociation, Inherited);
  216.  
  217. //----------------------------------------------------------------------------------------
  218. // TAssociation constructor
  219. //----------------------------------------------------------------------------------------
  220. #pragma segment MAAssociationRes
  221.  
  222. TAssociation::TAssociation()
  223. {
  224.     fEntries = NULL;
  225. } // TAssociation::TAssociation
  226.  
  227. //----------------------------------------------------------------------------------------
  228. // TAssociation::IAssociation:
  229. //----------------------------------------------------------------------------------------
  230. #pragma segment MAAssociationRes
  231.  
  232. void TAssociation::IAssociation()
  233. {
  234.     this->IObject();
  235.  
  236.     FailInfo fi;
  237.     Try(fi)
  238.     {
  239.         TEntriesList * anEntriesList;
  240.         anEntriesList = new TEntriesList;
  241.         anEntriesList->IEntriesList();
  242.         fEntries = anEntriesList;
  243. #if qDebug
  244.         fEntries->SetEltType("TEntry");
  245. #endif
  246.  
  247.         fi.Success();
  248.     }
  249.     else
  250.     {
  251.         this->Free();
  252.         fi.ReSignal();
  253.     }
  254. } // TAssociation::IAssociation
  255.  
  256. //----------------------------------------------------------------------------------------
  257. // TAssociation::Free:
  258. //----------------------------------------------------------------------------------------
  259. #pragma segment MAAssociationRes
  260.  
  261. TAssociation::~TAssociation()
  262. {
  263.     fEntries = (TEntriesList *)FreeListIfObject(fEntries);
  264. } // TAssociation::Free
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // TAssociation::EntryWithKey:
  268. //----------------------------------------------------------------------------------------
  269. #pragma segment MAAssociationRes
  270.  
  271. TEntry* TAssociation::EntryWithKey(const CStr255& keyStr)
  272. {
  273.     CEntryWithKey aCEntryWithKey(keyStr);
  274.  
  275.     return (TEntry *)(fEntries->Search(&CompareEntryKeys, &aCEntryWithKey));
  276. } // TAssociation::EntryWithKey
  277.  
  278. //----------------------------------------------------------------------------------------
  279. // TAssociation::EntryWithValue:
  280. //----------------------------------------------------------------------------------------
  281. #pragma segment MAAssociationRes
  282.  
  283. TEntry* TAssociation::EntryWithValue(const CStr255& valueStr)
  284. {
  285.     TEntry* returnEntry = NULL;
  286.     CArrayIterator iter(fEntries);
  287.     
  288.     for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  289.     {
  290.         TEntry* anEntry = (TEntry*)fEntries->At(i);
  291.         
  292.         if (valueStr == **(anEntry->fValue))
  293.         {
  294.             returnEntry = anEntry;
  295.             break;
  296.         }
  297.     }
  298.     return returnEntry;
  299. } // TAssociation::EntryWithValue
  300.  
  301. //----------------------------------------------------------------------------------------
  302. // TAssociation::InsertEntry:
  303. //----------------------------------------------------------------------------------------
  304. #pragma segment MAAssociationRes
  305.  
  306. void TAssociation::InsertEntry(const CStr255& keyStr,
  307.                                       const CStr255& valueStr)
  308. {
  309.     TEntry * anEntry;
  310.  
  311.     anEntry = this->EntryWithKey(keyStr);
  312.     if (anEntry)
  313.     {
  314.         anEntry->SetValue(valueStr);
  315.     }
  316.     else
  317.     {
  318.         anEntry = new TEntry;
  319.         anEntry->IEntry(keyStr, valueStr);
  320.         fEntries->Insert(anEntry);
  321.     }
  322. } // TAssociation::InsertEntry
  323.  
  324. //----------------------------------------------------------------------------------------
  325. // TAssociation::KeyAt:
  326. //----------------------------------------------------------------------------------------
  327. #pragma segment MAAssociationRes
  328.  
  329. Boolean TAssociation::KeyAt(const CStr255& valueStr,
  330.                                    CStr255& keyStr)
  331. {
  332.     Boolean returnVal = FALSE;
  333.     keyStr.Empty();
  334.  
  335.     TEntry * theEntry = NULL;
  336.     CArrayIterator iter(fEntries);
  337.     
  338.     for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  339.     {
  340.         TEntry* anEntry = (TEntry*)fEntries->At(i);
  341.         
  342.         if (valueStr == **(anEntry->fValue))
  343.         {
  344.             theEntry = anEntry;
  345.             break;
  346.         }
  347.     }
  348.     
  349.     if (theEntry)
  350.     {
  351.         keyStr = **((String255Handle)(theEntry->fKey));
  352.         returnVal = TRUE;
  353.     }
  354.     
  355.     return returnVal;
  356. } // TAssociation::KeyAt
  357.  
  358. //----------------------------------------------------------------------------------------
  359. // TAssociation::RemoveValueAt:
  360. //----------------------------------------------------------------------------------------
  361. #pragma segment MAAssociationRes
  362.  
  363. void TAssociation::RemoveValueAt(const CStr255& keyStr)
  364. {
  365.     TEntry * theEntry;
  366.     CEntryWithKey aCEntryWithKey(keyStr);
  367.  
  368.     theEntry = (TEntry *)(fEntries->Search(&CompareEntryKeys, &aCEntryWithKey));
  369.     if (theEntry)
  370.         fEntries->Delete(theEntry);
  371. } // TAssociation::RemoveValueAt
  372.  
  373. //----------------------------------------------------------------------------------------
  374. // TAssociation::RemoveKeyAt:
  375. //----------------------------------------------------------------------------------------
  376. #pragma segment MAAssociationRes
  377.  
  378. void TAssociation::RemoveKeyAt(const CStr255& valueStr)
  379. {
  380.     TEntry * theEntry = NULL;
  381.     CArrayIterator iter(fEntries);
  382.     
  383.     for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  384.     {
  385.         TEntry* anEntry = (TEntry*)fEntries->At(i);
  386.         
  387.         if (valueStr == **(anEntry->fValue))
  388.         {
  389.             theEntry = anEntry;
  390.             break;
  391.         }
  392.     }
  393.     if (theEntry)
  394.         fEntries->Delete(theEntry);
  395. } // TAssociation::RemoveKeyAt
  396.  
  397. //----------------------------------------------------------------------------------------
  398. // TAssociation::ValueAt:
  399. //----------------------------------------------------------------------------------------
  400. #pragma segment MAAssociationRes
  401.  
  402. Boolean TAssociation::ValueAt(const CStr255& keyStr,
  403.                                      CStr255& valueStr)
  404. {
  405.     Boolean returnVal = FALSE;
  406.     valueStr.Empty();
  407.  
  408.     CEntryWithKey aCEntryWithKey(keyStr);
  409.  
  410.     TEntry * theEntry = (TEntry *)(fEntries->Search(&CompareEntryKeys, &aCEntryWithKey));
  411.     if (theEntry)
  412.     {
  413.         valueStr = **((String255Handle)(theEntry->fValue));
  414.         returnVal = TRUE;
  415.     }
  416.     
  417.     return returnVal;
  418. } // TAssociation::ValueAt
  419.  
  420. //----------------------------------------------------------------------------------------
  421. // End of UAssociation.cp
  422.  
  423. #pragma segment Inline
  424.